package com.kaifamiao.controller;

import com.kaifamiao.pojo.Department;
import com.kaifamiao.pojo.Doctor;
import com.kaifamiao.service.imp.DepartmentService;
import com.kaifamiao.service.imp.DoctorService;
import com.kaifamiao.service.imp.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class DepartmentController {
    @Autowired
    DepartmentService service;
    @Autowired
    DoctorService docservice;
    @Autowired
    PatientService patientService;
    @RequestMapping("/find")
    public String findAll(Model model){
        List<Department> list = service.findAll();
        model.addAttribute("departmentlist",list);
        return "Patientinfohtml/find";
    }
//    ID查找数据传入修改页面
    @RequestMapping("/findById")
    public String selectById(@PathVariable Integer id,Model model){
        Department department = service.selectById(id);
        model.addAttribute("department",department);
        return "Patientinfohtml/edit";
    }
//    修改页面功能实现
    @RequestMapping("/update")
    public String update(Department department){
        if(department!=null) {
            Department departmentold = service.selectById(department.getDepartid());
            departmentold.setDepartname(department.getDepartname());
            departmentold.setDepartintro(department.getDepartintro());
            service.update(department);
        }
        return "Patientinfohtml/find";
    }
//    删除该科室
    @RequestMapping("/delete")
    public String delete(@PathVariable Integer id,Model model){
        if(docservice.findByDepartmentId(id)==null){
            service.delete(id);
        }else{
            model.addAttribute("deleteerr","该科室下还存在医生，无法删除");
        }
        return "Patientinfohtml/find";
    }
//    添加页面显示
    @RequestMapping("/addPage")
    public String add(){
        return "Patientinfohtml/add";
    }
//    添加功能实现
    @RequestMapping("/add")
    public String add(Department department,List<Doctor> doctor){
        service.add(department,doctor);
        return "Patientinfohtml/find";
    }
//    医院状况
    @RequestMapping("/aa/{id}")
    public String selectDoctorAndDepart(Model model, @PathVariable int id){
        List<Department> list = service.selectDoctorAndDepart(id);
        model.addAttribute("DoctorcountById",list.size());
        model.addAttribute("DoctorCount",docservice.findAll().size());
        model.addAttribute("patientCount",patientService.selectPatientInfoCount());
        model.addAttribute("CountNum",docservice.maxPatientsSum());

        return "Patientinfohtml/find";
    }
}
